<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-box">
      <my-seacher @click="goTosearch"></my-seacher>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航区域 -->
    <view class="nav_list">
      <view class="nav_item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src" class="nav_img"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor_list">
      <view class="floor_item" v-for="(item,i) in floorList" :key="i">
        <image :src="item.floor_title.image_src" class="floor_item_title"></image>
        <view class="floor_box">
          <!-- 左边图片 -->
          <navigator class="floor_box_left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右边的多个图片 -->
          <view class="floor_box_right">
            <navigator class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0"
              :url="item2.url">
              <image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: [],
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },

    methods: {
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        // 3.2 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()
        // 3.3 请求成功，为 data 中的数据赋值
        this.swiperList = res.message
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        // 3.2 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()
        // 3.3 请求成功，为 data 中的数据赋值
        this.navList = res.message
      },
      navClickHandler(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        // 3.2 请求失败
        if (res.meta.status !== 200) return uni.$showMsg()
        // 通过双层 forEach 循环，处理 URL 地址
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        // 3.3 请求成功，为 data 中的数据赋值
        this.floorList = res.message
      },
      goTosearch() {
        console.log('55555555555555')
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    swiper-item {
      width: 100%;
      height: 100%;

      image {
        width: 100%;
      }
    }
  }

  .nav_list {
    margin: 30rpx 0;
    display: flex;
    justify-content: space-around;

    .nav_img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor_item {
    .floor_item_title {
      width: 100%;
      height: 60rpx;
    }

    .floor_box {
      display: flex;
      justify-content: space-around;
      padding: 15rpx;

      .floor_box_right {
        display: flex;
        justify-content: space-around;
        flex-wrap: wrap;
      }
    }
  }
  .search-box{
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
